﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using Yipusoft.BasicBase.BLL;
using Yipusoft.BasicBase.Model;
using System.Text;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;

public partial class BasicBase_DeviceSearch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //BindDepartment();
            ActionTitle.Text = "装备器材查询";
            ListItem li = new ListItem();
            li.Text = "";
            li.Value = "";
            DeviceClassList.Items.Add(li);
            StateList.Items.Add(li);
            // BindCodeItemList(DeviceClassList, "ZBQC010000");
            BindCodeItemList(UnitNameList, "ZBQC020000");
            BindCodeItemList(StateList, "ZBQC030000");
            BindType();

            BindClass();
        }
    }

    //void BindDepartment()
    //{
    //    UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
    //    DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
    //    if (userInfo.DepartmentID.Substring(0, 4) == "1300")
    //    {
    //        DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') and DepartmentID<>'1300000' ");
    //        DepartmentList.DataBind();
    //        DepartmentListBind();
    //    }
    //    else
    //    {
    //        DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
    //        DepartmentList.DataBind();
    //        DepartmentListBind();
    //    }
    //}
    private void BindCodeItemList(DropDownList ddl, string parentID)
    {
        ListItem li;
        List<CodeItemInfo> items = CodeItem.GetList("ParentID='" + parentID + "' ORDER BY ItemID,SeqID");
        foreach (CodeItemInfo item in items)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * (item.ItemLevel - 1), '　') + item.ItemName;
            li.Value = item.ItemID;
            ddl.Items.Add(li);
            BindCodeItemList(ddl, item.ItemID);
        }
    }
    protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        string Where = "";
        //部门
        //if (UnitList.SelectedValue != "")
        //{
        //    Where += Where.Length == 0 ? "" : " and ";
        //    Where = " departmentid='" + UnitList.SelectedValue + "' ";
        //}
        //else
        //{
        //    if (DepartmentList.SelectedValue == "")
        //    {
        //        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        //        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        //        if (userInfo.DepartmentID.Substring(0, 4) != "1300")
        //        {
        //            Where += Where == "" ? "" : " And ";
        //            Where += " DepartmentID like '" + userInfo.DepartmentID.Substring(0, 4) + "%' ";
        //        }
        //    }
        //    else
        //    {
        //        Where += Where == "" ? "" : " And ";
        //        Where += " DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' ";
        //    }
        //}

        if (txtVaule.Value != "")
        {
            string strDepartment = "";
            string[] departments = txtVaule.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < departments.Count(); i++)
            {
                strDepartment += "'" + departments[i] + "',";
            }

            strDepartment = " departmentid in (" + strDepartment.Substring(0, strDepartment.Length - 1) + ")";
            Where += strDepartment;
        }
        else
        {
            //部门
            UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
            DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
            if (userInfo.DepartmentID.Substring(0, 4) != "1300")
            {
                Where = " departmentid like '" + department.DepartmentID.Substring(0, 4) + "%' ";
            }
        }

        //类型
        if (DeviceTypeList.SelectedValue != "")
        {
            if (DeviceClassList.SelectedValue == "")
            {
                Where += Where.Length == 0 ? "" : " and ";
                Where += " deviceclass like '" + DeviceTypeList.SelectedValue.Substring(0, 8) + "__'";
            }
            else

                if (DeviceClassList.SelectedValue != "")
                {
                    Where += Where.Length == 0 ? "" : " and ";
                    CodeItemInfo item = CodeItem.GetCodeItem(DeviceClassList.SelectedValue);
                    if (item.ItemLevel == 0)
                        Where += " left(deviceclass,6)='" + DeviceClassList.SelectedValue.Substring(0, 6) + "' ";
                    if (item.ItemLevel == 1)
                        Where += " left(deviceclass,8)='" + DeviceClassList.SelectedValue.Substring(0, 8) + "' ";
                    if (item.ItemLevel == 2)
                        Where += " deviceclass='" + DeviceClassList.SelectedValue + " '";

                }
        }
        //名称
        if (DeviceName.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " DeviceName like '%" + DeviceName.Text + "%' ";
        }
        //型号
        if (DeviceModel.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " DeviceModel like '%" + DeviceModel.Text + "%'";
        }
        //数量
        if (DeviceCount.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " DeviceCount " + DeviceCountList.SelectedValue + " " + DeviceCount.Text + " ";
            if (UnitNameList.SelectedValue != "")
            {
                Where += " and UnitName='" + UnitNameList.SelectedValue + "' ";
            }

        }
        //参数
        if (Parameters.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " Parameters='%" + Parameters.Text + "%' ";
        }

        //价格
        if (Price.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " Price " + PriceList.SelectedValue + " " + Price.Text + " ";
        }

        //购入时间
        if (DatePurchaseStart.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " DatePurchase >='" + DatePurchaseStart.Text + "' ";
        }

        if (DateProductEnd.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " DatePurchase <'" + Convert.ToDateTime(DateProductEnd.Text).AddDays(1) + "' ";
        }

        //使用日期
        if (DateUseStart.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " DateUse >='" + DateUseStart.Text + "' ";
        }

        if (DateUseEnd.Text.Length > 0)
        {

            Where += Where.Length == 0 ? "" : " and ";
            Where += " DateUse <'" + Convert.ToDateTime(DateUseEnd.Text).AddDays(1) + "' ";
        }

        //出厂日期
        if (DateProductStart.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " DateProduct>= '" + DateProductStart.Text + "'";
        }
        if (DateProductEnd.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " DateProduct< '" + Convert.ToDateTime(DateProductEnd.Text).AddDays(1) + "'";
        }
        if (Manufacturer.Text.Length > 0)
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " Manufacturer like '%" + Manufacturer.Text + "%' ";
        }
        //状态
        if (StateList.SelectedValue != "")
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " State='" + StateList.SelectedValue + "' ";
        }
        //图片
        if (ImageList.SelectedValue == "1")
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " Picture <> '' ";
        }
        if (ImageList.SelectedValue == "0")
        {
            Where += Where.Length == 0 ? "" : " and ";
            Where += " Picture = '' ";
        }
        Where = Server.UrlEncode(Where);
        Response.Redirect("DeviceResult.aspx?Where=" + Where);
    }

    //protected void DepartmentList_TextChanged(object sender, EventArgs e)
    //{
    //    DepartmentListBind();
    //}

    //void DepartmentListBind()
    //{
    //    if (DepartmentList.SelectedValue == "")
    //    {
    //        UnitList.Items.Clear();
    //        UnitList.Items.Add(new ListItem("------------部门-----------------------", ""));
    //        UnitList.DataSource = null;
    //        UnitList.DataBind();
    //    }
    //    else
    //    {
    //        UnitList.Items.Clear();
    //        UnitList.Items.Add(new ListItem("------------部门-----------------------", ""));
    //        string strWhere = "DepartmentID like '" + DepartmentList.SelectedValue.Substring(0, 4) + "%' and   SUBSTRING(DepartmentID, 5, 1)<>'0'";
    //        UnitList.DataSource = Department.GetList(strWhere);
    //        UnitList.DataBind();
    //    }
    //}

    void BindType()
    {
        DeviceTypeList.DataTextField = "ItemName";
        DeviceTypeList.DataValueField = "ItemID";
        DeviceTypeList.DataSource = CodeItem.GetList(" parentID='ZBQC010000' order by ItemID,SeqID ");
        DeviceTypeList.DataBind();
    }
    protected void DeviceTypeList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindClass();
    }

    private void BindClass()
    {
        if (DeviceTypeList.SelectedValue != "")
        {
            DeviceClassList.Items.Clear();
            DeviceClassList.Items.Add(new ListItem("------------类别-----------------------", ""));
            DeviceClassList.DataTextField = "ItemName";
            DeviceClassList.DataValueField = "ItemID";
            DeviceClassList.DataSource = CodeItem.GetList(" parentID='" + DeviceTypeList.SelectedValue + "' order by ItemID,SeqID ");
            DeviceClassList.DataBind();
        }
        else
        {
            DeviceClassList.Items.Clear();
            DeviceClassList.Items.Add(new ListItem("------------类别-----------------------", ""));
            DeviceClassList.DataSource = "";
            DeviceClassList.DataBind();
        }
    }
}
